package com.chen.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.base.utils.R;
import com.chen.dto.StudentDTO;
import com.chen.dto.StudentExamChartDTO;
import com.chen.dto.StudentLoginDTO;
import com.chen.dto.StudentRegisterDTO;
import com.chen.entity.Classes;
import com.chen.entity.Student;
import com.chen.mapper.ClassesMapper;
import com.chen.mapper.StudentMapper;
import com.chen.service.StudentService;
import com.chen.vo.ClassesVO;
import com.chen.vo.StudentLoginVO;
import com.chen.vo.StudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 学生相关服务实现
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    // 班级相关
    @Autowired
    private ClassesMapper classesMapper;

    /**
     * 检查学生登陆账号是否正确
     * @param account
     * @return
     */
    @Override
    public R validateLogin(String account) {
        Assert.isTrue(ObjectUtil.isNotEmpty(account), "账号不能为空");
        Student student = baseMapper.findByAccount(account);
        if (ObjectUtil.isNotEmpty(student)) {
            return R.ok().data(student.getPwd());
        }
        return R.ok().data("n");
    }

    /**
     * 学生登陆相关实现
     * @param studentLoginDTO
     * @return
     */
    @Override
    public StudentLoginVO studentLogin(StudentLoginDTO studentLoginDTO) {
        // 学生账号
        String studentAccount = studentLoginDTO.getStudentAccount();
        String studentPwd = studentLoginDTO.getStudentPwd();
        if (ObjectUtil.isNotEmpty(studentAccount) && ObjectUtil.isNotEmpty(studentPwd)) {
            return baseMapper.findByAccountAndPwdLogin(studentAccount, studentPwd);
        }
        return null;
    }

    /**
     * 检查学生账号是否存在
     * @param account
     * @return
     */
    @Override
    public boolean checkAccountIsExists(String account) {
        Assert.isTrue(ObjectUtil.isNotEmpty(account), "检查的学生账号为空");
        Student student = baseMapper.findByAccount(account);
        return ObjectUtil.isNotEmpty(student);
    }

    @Override
    public boolean studentRegister(StudentRegisterDTO studentRegisterDTO) {
        // 注册学生姓名
        String name = studentRegisterDTO.getName();
        Assert.isTrue(ObjectUtil.isNotEmpty(name), "注册学生姓名不能为空");
        // 注册学生账号
        String account = studentRegisterDTO.getAccount();
        Assert.isTrue(ObjectUtil.isNotEmpty(account), "注册学生账号不能为空");
        // 注册学生密码
        String pwd = studentRegisterDTO.getPwd();
        Assert.isTrue(ObjectUtil.isNotEmpty(pwd), "注册学生密码不能为空");
        // 注册学生班级
        Integer classId = studentRegisterDTO.getClassId();
        Assert.isTrue(ObjectUtil.isNotEmpty(classId), "注册学生班级不能为空");
        Student chectStudent = baseMapper.findByAccount(account);
        Assert.isTrue(ObjectUtil.isEmpty(chectStudent), "该注册学生账号已被占用");
        Classes classes = classesMapper.selectById(classId);
        Assert.isTrue(ObjectUtil.isNotEmpty(classes), "班级信息异常");
        // 将要注册的学生信息
        Student registerStudent = Student.builder().name(name).account(account).pwd(pwd).classesId(classId).build();
        // 插入到数据库
        baseMapper.insert(registerStudent);
        return true;
    }

    /**
     * 学生查询个人信息
     * @param studentId
     * @return
     */
    @Override
    public StudentVO selfInfo(Integer studentId) {
        Assert.isTrue(ObjectUtil.isNotEmpty(studentId), "学生id不能为空");
        Student student = baseMapper.selectById(studentId);
        if (ObjectUtil.isNotEmpty(student)) {
            StudentVO studentVO = BeanUtil.toBean(student, StudentVO.class);
            Classes classes = classesMapper.selectById(student.getClassesId());
            if (ObjectUtil.isNotEmpty(classes)) {
                ClassesVO classesVO = BeanUtil.toBean(classes, ClassesVO.class);
                studentVO.setClasses(classesVO);
            }
            return studentVO;
        }
        return null;
    }

    @Override
    public List<StudentVO> getStudentsByClassId(Integer classId) {
        List<Student> studentList = baseMapper.findByClassesId(classId);
        if (CollectionUtil.isNotEmpty(studentList)) {
            List<StudentVO> studentVOS = BeanUtil.copyToList(studentList, StudentVO.class);
            for (StudentVO studentVO : studentVOS) {
                Classes classes = classesMapper.selectById(studentVO.getClassesId());
                if (ObjectUtil.isNotEmpty(classes)) {
                    ClassesVO classesVO = BeanUtil.toBean(classes, ClassesVO.class);
                    studentVO.setClasses(classesVO);
                }
            }
            return studentVOS;
        }

        return Collections.emptyList();
    }

    @Override
    public Long findAllCount() {
        return baseMapper.findAllCount();
    }

    @Override
    public List<StudentVO> listPage(Integer id, Integer classId, Integer gradeId, Integer startIndex, Integer pageShow) {
        return baseMapper.listPage(id, classId, gradeId, startIndex, pageShow);
    }

    /**
     * 根据学生id获取学生详细信息
     * @param studentId
     * @return
     */
    @Override
    public StudentVO getStudentById(Integer studentId) {
        return baseMapper.getStudentById(studentId);
    }

    /**
     * 修改学生信息
     * @param studentDTO
     * @return
     */
    @Override
    public Integer updateStudent(StudentDTO studentDTO) {
        Integer id = studentDTO.getId();
        Assert.isTrue(ObjectUtil.isNotEmpty(id), "学生id不能为空");
        Student student = BeanUtil.toBean(studentDTO, Student.class);
        return baseMapper.updateById(student);
    }


    @Override
    public Integer delStudent(Integer studentId) {
        Assert.isTrue(ObjectUtil.isNotEmpty(studentId), "学生id不能为空");
        return baseMapper.deleteById(studentId);
    }

    /**
     * 图标数据
     * @param classId
     * @return
     */
    @Override
    public List<StudentExamChartDTO> getStudentsByClassIdCharData(Integer classId) {
        return baseMapper.getStudentsByClassIdCharData(classId);
    }

    /**
     * 重置学生密码
     * @param id
     * @param pwd
     * @return
     */
    @Override
    public Integer resetPwd(Integer id, String pwd) {
        if (ObjectUtil.isNotEmpty(id) && ObjectUtil.isNotEmpty(pwd)) {
            Student student = baseMapper.selectById(id);
            if (ObjectUtil.isNotEmpty(student)) {
                student.setPwd(pwd);
                return baseMapper.updateById(student);
            }
        }
        return 0;
    }
}

